package homelesspartners.client.widgets;

import homelesspartners.client.model.City;

import java.util.Iterator;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;


public class CitiesWidget extends Composite {

	private VerticalPanel verticalPanel = new VerticalPanel();
	private HTML heading = new HTML("&nbsp;<b>STEP 1 - Choose City</b><br/><br/>");
	private HTML subText = new HTML("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please choose your city from the list below:<br/><br/>");
	
	private static CitiesWidget instance = new CitiesWidget();
	
	public static CitiesWidget getInstance() {
		return instance;
	}

	public CitiesWidget() {
		verticalPanel.add(heading);
		verticalPanel.add(subText);
		initWidget(verticalPanel);
	}
	
	public boolean isPopulated(){
		if(City.getCities() == null){
			return false;
		}
		return true;
	}
	
	
	public void refresh() {
		verticalPanel.clear();
		verticalPanel.add(heading);
		verticalPanel.add(subText);
		
		if(City.getCities() == null){
			verticalPanel.add(new Label("Loading the list of cities, please wait..."));
			return;
		} else if(City.getCities().size() == 0){
			verticalPanel.add(new Label("No cities are available on this site yet, please try again later."));
		}
		
		for (Iterator cities = City.getCities().iterator(); cities.hasNext();) {
			City city = (City) cities.next();
			CityWidget cityWidget = new CityWidget(city);
			verticalPanel.add(cityWidget);
		}
		
		verticalPanel.add(new HTML("<br /><br /><br />"));
	}

	
}
